package com.herench.bs.rtconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


/**
 * @author luoxiaoming 2018/12/1
 *         spring mvc 配置类
 */

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 视图解析器注册
     *
     * @return 视图解析器
     * @throws Exception
     */
    @Bean
    public ViewResolver viewResolver() throws Exception {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(Class.forName(org.springframework.web.servlet.view.JstlView.class.getName()));
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    /**
     * 文件解析器注册
     *
     * @return 文件解析器
     */
    @Bean
    public MultipartResolver multipartResolver() {
        /**
         * 使用通用文件解析器
         */
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        /**
         * 设置上传文件最大阀值
         */
        multipartResolver.setMaxUploadSize(5000000l);
        return multipartResolver;
    }

    /**
     * 注册静态资源处理器
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/extjs/**").addResourceLocations("/extjs/");
        registry.addResourceHandler("/console/**").addResourceLocations("/console/");
        registry.addResourceHandler("/console.html").addResourceLocations("/console.html");
        registry.addResourceHandler("/index.html").addResourceLocations("/index.html");
    }

}
